#include <i386/string.h>

#ifndef malloc
	void *malloc(unsigned int);
#endif

char *strcpy(char *dst,const char *src) {
	char *retval = dst;
	while (*src != 0) {
		*dst++ = *src++;
	}
	
	*dst = 0;
	return retval;
}

char *strdup( const char *str ) {
	char *p;
	if( !str ) {
		return NULL;
	}
	
	p = malloc( strlen(str) + 1 );
	strcpy( p, str );
	
	return p;
}

char *strncpy(char *dst,const char *src,int n) {
	char *retval = dst;
	while (*src != 0 && n-- > 0) {
		*dst++ = *src++;
	}
	
	*dst = 0;
	return retval ;
}

int strcmp(const char *s1,const char *s2) {
	while (*s1 == *s2) {
		if (*s1 == 0) {
			return 0;
		}
		
		s1++;
		s2++;
	}
	
	return *(unsigned const char *)s1 - *(unsigned const char *)(s2);
}

int strcasecmp( const char *cs, const char *ct ) {
	register signed char res;

	while (1) {
		char ch1 = toupper(*cs), ch2 = toupper(*ct);
		ct++;
		if ((res = ch1 - ch2) != 0 || !*cs++) {
			break;
		}
	}
	
	return res;
}

int strncmp(const char *s1, const char *s2, int n) {
	if (n == 0) {
		return 0;
	}
	
	do {
		if (*s1 != *s2++) {
		    return *(unsigned const char *)s1 - *(unsigned const char *)--s2;
		}
		
		if (*s1++ == 0) {
			break;
		}
	} while (--n != 0);
	
	return 0;
}

char *strupr(char *s) {
	char *base = s;
	
	while (*s != 0) {
		if (*s >= 'a' && *s <= 'z') {
			*s = *s + 'A' -'a';
		}
		
		s++;
	}
	
	return base;
}

char *strlwr(char *s) {
	char *base = s;
	
	while (*s != 0) {
		if (*s >= 'A' && *s <= 'Z') {
			*s = *s + 'a' -'A';
		}
		
		s++;
	}
	
	return base;
}

char * strpbrk(const char * cs,const char * ct) {
	const char *sc1,*sc2;

	for( sc1 = cs; *sc1 != '\0'; ++sc1) {
		for( sc2 = ct; *sc2 != '\0'; ++sc2) {
			if (*sc1 == *sc2) {
				return (char *) sc1;
			}
		}
	}
	return NULL;
}

int strlen(const char *s) {
	register int result = 0;
	
	while (*s != 0) {
		s++;
		result++;
	}
	
	return result;
}

char *strcat(char *dst,const char *src) {
	char *base = dst;
	
	while (*dst != 0) {
		dst++;
	}
	
	while (*src != 0) {
		*dst++ = *src++;
	}
	
	*dst = 0;
	return base;
}

char *strncat(char *dst,const char *src,int n) {
	char *base = dst;
	
	while (*dst != 0) {
		dst++;
	}
	
	while (*src != 0 && n != 0) {
		*dst++ = *src++;
		n--;
	}
	
	*dst = 0;
	return base;
}

char *strscn(char *s,char *pattern) {
	char *scan;
	
	while (*s != 0) {
		scan = pattern;
		
		while (*scan != 0) {
			if (*s == *scan) {
				return s;
			} else {
				scan++;
			}
		}
		
		s++;
	}
	
	return NULL;
}

char *strchr(const char *s,int c) {
	while (*s != 0) {
	if (*s == (char)(c)) return((char *) s);
	else s++;
	}
	return(NULL);
}

char * strstr(const char * s1, const char * s2) {
	int l1, l2;

	l2 = strlen(s2);
	if (!l2) {
		return (char *) s1;
	}
	
	l1 = strlen(s1);
	while (l1 >= l2) {
		l1--;
		
		if (!memcmp(s1, s2, l2)) {
			return (char *) s1;
		}
		
		s1++;
	}
	
	return NULL;
}

char * strsep(char **s, const char *ct) {
	char *sbegin = *s, *end;

	if (sbegin == NULL) {
		return NULL;
	}

	end = strpbrk(sbegin, ct);
	if (end) {
		*end++ = '\0';
	}
	
	*s = end;

	return sbegin;
}

unsigned char _ctype[] = {
_C,_C,_C,_C,_C,_C,_C,_C,								/* 0-7 */
_C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C,					/* 8-15 */
_C,_C,_C,_C,_C,_C,_C,_C,								/* 16-23 */
_C,_C,_C,_C,_C,_C,_C,_C,								/* 24-31 */
_S|_SP,_P,_P,_P,_P,_P,_P,_P,							/* 32-39 */
_P,_P,_P,_P,_P,_P,_P,_P,								/* 40-47 */
_D,_D,_D,_D,_D,_D,_D,_D,								/* 48-55 */
_D,_D,_P,_P,_P,_P,_P,_P,								/* 56-63 */
_P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U,				/* 64-71 */
_U,_U,_U,_U,_U,_U,_U,_U,								/* 72-79 */
_U,_U,_U,_U,_U,_U,_U,_U,								/* 80-87 */
_U,_U,_U,_P,_P,_P,_P,_P,								/* 88-95 */
_P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L,				/* 96-103 */
_L,_L,_L,_L,_L,_L,_L,_L,								/* 104-111 */
_L,_L,_L,_L,_L,_L,_L,_L,								/* 112-119 */
_L,_L,_L,_P,_P,_P,_P,_C,								/* 120-127 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,						/* 128-143 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,						/* 144-159 */
_S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,	/* 160-175 */
_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,		/* 176-191 */
_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,		/* 192-207 */
_U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L,		/* 208-223 */
_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,		/* 224-239 */
_L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L};		/* 240-255 */
